package com.gs.synedocket.db.dao;

import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.db.domain.LogData;
import com.gs.synedocket.db.domain.LogDataDao;
import com.gs.synedocket.util.SPUtil;

import java.util.List;

public class LogUtil {
    private static volatile LogUtil mInstance;
    private CommonDaoUtil<LogData> mLogUtil;

    public static LogUtil getInstance() {
        if (mInstance == null) {
            synchronized (LogUtil.class) {
                if (mInstance == null) {
                    mInstance = new LogUtil();
                }
            }
        }
        return mInstance;
    }

    public LogUtil() {
        LogDataDao logDataDao = DBUtil.getInstance().getWriteDaoSession().getLogDataDao();
        mLogUtil = new CommonDaoUtil<>(LogData.class, logDataDao);
    }

    public long insertOrReplaceData(String msg) {
        String userId = SPUtil.getInstance().getString(AllConstants.ID);
        if (userId.isEmpty()) {
            return -1;
        }
        LogData logData = new LogData();
        logData.setMessage(msg);
        logData.setLogAt(System.currentTimeMillis());
        logData.setLogType(AllConstants.LOG_TYPE);
        logData.setUserId(userId);
        return mLogUtil.insertOrReplace(logData);
    }


    public void update(LogData logData) {
        mLogUtil.update(logData);
    }

    public List<LogData> getData(int limit) {
        return mLogUtil.query(LogDataDao.Properties.LogAt, limit);
    }

    public long getDataCount() {
        return mLogUtil.getCount();
    }

    public void delete(long id) {
        mLogUtil.delete(id);
    }

    public void delete(String ids) {
        mLogUtil.delete("DELETE FROM LOG_DATA WHERE _id IN (" + ids + ")");
    }
}
